import { Component, ChangeDetectionStrategy, OnInit, OnDestroy, ViewChild } from '@angular/core';
import { Subscription } from 'rxjs';
import { STComponent, STData, STColumn, STChange, STColumnTag } from '@delon/abc';
import { RouteService, EmitterService } from '@core';
import { ActivatedRoute, Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';

const BADGE_MSGTYPE: STColumnTag = {
  SYS: { text: '系统', color: 'red' },
  USER: { text: '用户', color: 'blue' },
};

@Component({
  selector: 'notification-list',
  templateUrl: './notific-list.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NotificListComponent implements OnInit, OnDestroy {
  private route$: Subscription;
  @ViewChild('st', { static: true }) private st: STComponent;
  checkList: STData[] = [];
  pi = 1;
  params: any = {};
  url = '/cf/admin/web-msg/get';
  columns: STColumn[] = [
    { title: '', type: 'checkbox' },
    { title: '标题', render: 'title' },
    { title: '时间', index: 'createDate', type: 'date' },
    { title: '类型', index: 'msgType', type: 'tag', tag: BADGE_MSGTYPE },
    {
      title: '子类型',
      index: 'sysType',
      format: (item: STData, col: STColumn, index: number) => {
        if (item.sysType === 'JOB_EXCEPTION') {
          return '作业异常';
        } else if (item.sysType === 'SYSTEM_UPDATE') {
          return '系统更新';
        } else if (item.sysType === 'LOGIN_EXCEPTION') {
          return '登录异常';
        } else {
          if (item.sysType) {
            return '其它';
          }
          return '';
        }
      },
    },
  ];

  get types() {
    return ['全部类型消息', '用户', '系统'];
  }

  constructor(
    routeStateSrv: RouteService,
    private route: ActivatedRoute,
    private router: Router,
    public msg: NzMessageService,
    private http: _HttpClient,
    private emitSrv: EmitterService,
  ) {
    this.pi = routeStateSrv.pi;
  }

  ngOnInit() {
    this.route$ = this.route.params.subscribe(p => {
      this.params.isRead = p.type;
      this.params.index = +p.index;
      this.updateType();
      if (this.st) {
        this.st.reload(this.params);
      }
    });
  }

  ngOnDestroy() {
    this.route$.unsubscribe();
  }

  private get checkIds() {
    return this.checkList.map(i => i.id);
  }

  change(e: STChange) {
    if (e.type === 'checkbox') {
      this.checkList = e.checkbox;
    }
  }

  remove() {
    this.http.post(`/cf/admin/web-msg/delete`, this.checkIds).subscribe(res => {
      this.st.reload();
      this.emitSrv.sendMessage({ type: 'regetUnread' });
    });
  }

  setRead() {
    this.http.post(`/cf/admin/web-msg/readed`, this.checkIds).subscribe(res => {
      this.st.reload();
      this.emitSrv.sendMessage({ type: 'regetUnread' });
    });
  }

  setAllRead() {
    this.http.post(`/cf/admin/web-msg/allreaded`).subscribe(res => {
      this.st.reload();
      this.emitSrv.sendMessage({ type: 'regetUnread' });
    });
  }

  private updateType() {
    const { params } = this;
    const text = this.types[params.index];
    if (text === '全部类型消息') {
      params.q = '';
    } else if (text === '用户') {
      params.q = 'USER';
    } else if (text === '系统') {
      params.q = 'SYS';
    }
  }

  changeType() {
    this.updateType();
    this.router.navigateByUrl(`/notification/inner/${this.params.isRead}/${this.params.index}`);
  }
}
